<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>区域管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-tree-txt.orange{
            color: #f56643 
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户状态</label>
                            <div class="layui-input-inline">
                                <select name="openStatus" lay-filter="openStatus">
                                    <option value=""></option>
                                    <option value="1">启用</option>
                                    <option value="0">禁用</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">类别</label>
                            <div class="layui-input-inline">
                                <select name="relationType" lay-filter="relationType">
                                    <option value="">全部</option>
                                    <option value="1">食堂</option>
                                    <option value="2">供应商</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary reReset" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <div class="layui-row">
                    <div class="layui-col-xs12 layui-col-sm3 layui-col-md3 layui-col-lg3">
                        <button class="layui-btn layui-btn-sm" onclick="window.location.href='areaManagementMaintenance.html'"><i class="layui-icon">&#xe642;</i>区域维护</button>
                        <div id="test1"></div>
                    </div>
                    <div class="layui-col-xs12 layui-col-sm9 layui-col-md9 layui-col-lg9">
                        <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
                    </div>
                </div>
            </div>
        </div>

    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'tree'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            tree = layui.tree;
        var excelList,currPage,regionId
		
		$('.reReset').click();

        //渲染
        var inst1
        var userInfo = localStorage.getItem('xi-can-management-userInfo')
        var userId = ''
        if(userInfo){
            userInfo = JSON.parse(userInfo)
            userId = userInfo.userInfo.id
            if(localStorage.getItem('xi-can-management-tree-'+userId)){
                var nodeList = JSON.parse(localStorage.getItem('xi-can-management-tree-'+userId))
                
                http.postJSON('admin/region/listAllRegionWithSpread',JSON.stringify(nodeList),function(res){
                    if(res.code == 200){
                        inst1 = tree.render({
                            elem: '#test1',  //绑定元素
                            data: res.data,
                            onlyIconControl: true,
                            click: function(obj){
                                $('.layui-tree-txt').removeClass('orange')
                                $(obj.elem).find('.layui-tree-txt').eq(0).addClass('orange')
                                regionId = obj.data.id
                                var param = {}
                                var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
                                var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
                                param.startTime = startTime;
                                param.endTime = endTime;
                                param.regionId = regionId
                                param.openStatus = $('select[name=openStatus]').val()
                                param.relationType = $('select[name=relationType]').val()
                                selectTable.reload({
                                    page:{curr:1},
                                    where: param
                                })
                            },
                            spread: function(obj){
                                // // console.log(obj)
                                if(obj.state == 0){ // 收起
                                    closeNode(obj.data.id)
                                }else if(obj.state == 1){ // 展开
                                    openNode(obj.data.id)
                                }
                            },
                            done: function() {
                                var tip_index
                                $(".layui-tree-txt").hover(function(){
                                    var level = $(this).data('level')
                                    // console.log(level)
                                    if(level == 2){
                                        var remarks = $(this).data('remarks')
                                        tip_index=layer.tips(remarks, this, {
                                            time:0
                                        });
                                    }
                                },function(){
                                    layer.close(tip_index);
                                });
                            }
                        });
                    }else{
                        layer.msg(res.msg)
                    }
                })

            }else{
                http.get('admin/region/listAllRegion',{},function(res){
                    if(res.code == 200){
                        inst1 = tree.render({
                            elem: '#test1'  //绑定元素
                            ,data: res.data
                            ,onlyIconControl: true
                            ,click: function(obj){
                                $('.layui-tree-txt').removeClass('orange')
                                $(obj.elem).find('.layui-tree-txt').eq(0).addClass('orange')
                                regionId = obj.data.id
                                var param = {}
                                var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
                                var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
                                param.startTime = startTime;
                                param.endTime = endTime;
                                param.regionId = regionId
                                param.openStatus = $('select[name=openStatus]').val()
                                param.relationType = $('select[name=relationType]').val()
                                selectTable.reload({
                                    page:{curr:1},
                                    where: param
                                })
                            },
                            spread: function(obj){
                                // // console.log(obj)
                                if(obj.state == 0){ // 收起
                                    closeNode(obj.data.id)
                                }else if(obj.state == 1){ // 展开
                                    openNode(obj.data.id)
                                }
                            },
                            done: function() {
                                var tip_index
                                $(".layui-tree-txt").hover(function(){
                                    var level = $(this).data('level')
                                    // console.log(level)
                                    if(level == 2){
                                        var remarks = $(this).data('remarks')
                                        tip_index=layer.tips(remarks, this, {
                                            time:0
                                        });
                                    }
                                },function(){
                                    layer.close(tip_index);
                                });
                            }
                        });
                    }else{
                        layer.msg(res.msg)
                    }
                })
            }

        }



        
        function closeNode(id){
            var nodeList = localStorage.getItem('xi-can-management-tree-'+userId)
            if(nodeList){
                nodeList = JSON.parse(nodeList)
                for(var i=0;i<nodeList.length;i++){
                    if(nodeList[i] == id){
                        nodeList.splice(i,1)
                        break
                    }
                }
                localStorage.setItem('xi-can-management-tree-'+userId,JSON.stringify(nodeList))
            }
        }
        function openNode(id){
            var nodeList = localStorage.getItem('xi-can-management-tree-'+userId)
            if(nodeList){
                nodeList = JSON.parse(nodeList)
                for(var i=0;i<nodeList.length;i++){
                    if(nodeList[i] == id){
                        nodeList.splice(i,1)
                        break
                    }
                }
            }else{
                nodeList = []
            }
            
            nodeList.push(id)
            localStorage.setItem('xi-can-management-tree-'+userId,JSON.stringify(nodeList))

        }


        // 后台用户列表
        var selectTable;
        selectTable = http.table({
            elem: '#selectTable',
            url: 'admin/region/listRelationSupplierAndCanteen',
            method: 'get',
            id: 'selectTable',
            toolbar: '#toolbarDemo',
            done: function(res, curr, count){
			    excelList = res.data;
                currPage = curr
			},
            cols: [[
                {title:'序号',type:'numbers',fixed: 'left'}
                ,{field:'name', title: '食堂名称/供应商名称', align : 'center'}
				,{field:'createTime', title: '创建时间', align : 'center',width: 170}
                ,{field:'openStatus', title: '状态', align : 'center',
					templet: function (d) {
						return d.openStatus==1?'启用':'禁用';
					}
				}
                ,{field:'relationType', title: '类别', align : 'center',
					templet: function (d) {
						return d.relationType==1?'食堂':'供应商';
                    }
                }
            ]]
        });

        //查询
        form.on('submit(searchBth)', function (data) {
            let _data = data.field,
				param = _data;
			var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
			var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
			param.startTime = startTime;
            param.endTime = endTime;
            param.regionId = regionId
			selectTable.reload({
				page:{curr:1},
				where: param
			})
        })

        // table的toolbar
        table.on('toolbar(selectTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data; //获取选中行数据
            if(obj.event=='add'){ 
                // 添加
                location.href = "backgroundAccountAdd.html"
            }else if(obj.event=='export'){ // 导出
				var data = excelList; 
				if (data.length == 0) {
					layer.msg('当前页没有可导出的数据');
					return;
                }
                var param = {
                    page: currPage,
                    limit: 10,
                    authentication: localStorage.getItem('xi-can-management-authentication'),
                    startTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'',
                    endTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'',
                    openStatus: $('select[name=openStatus]').val(),
                    relationType: $('select[name=relationType]').val(),
                    regionId: regionId
                }
                if (param) {  
                    let paramsArray = [];  
                    //encodeURIComponent  
                    Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                    window.open(TUrl+"admin/region/exportRelationSupplierAndCanteen?"+paramsArray.join('&'))
                }
                
			}
        })

    })

</script>
</html>